#include "auxiliaryfunction.h"



QByteArray AuxiliaryFunction::IntToArray(int v)
{
    int size = sizeof (int);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}

QByteArray AuxiliaryFunction::ShortToArray(unsigned short v)
{
    int size = sizeof (unsigned short);

    QByteArray a;
    a.resize(size);

    for(int i = 0; i < size; i++){
        a[i] = static_cast<char>( (v >> ( (size - i -1) * 8) ) & 0xff );
    }

    return a;
}

unsigned int AuxiliaryFunction::ByteArrayToUInt(const QByteArray &array)
{
    if (array.size() < static_cast<int>(sizeof(unsigned int))) {
            // 错误处理：数据长度不足
            return 0;
        }

        unsigned int value = 0;
        value |= static_cast<unsigned int>(array[0]);
        value |= static_cast<unsigned int>(array[1] << 8);
        value |= static_cast<unsigned int>(array[2] << 16);
        value |= static_cast<unsigned int>(array[3] << 24);
        return value;
}
